package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.base.BaseShop;
import com.ruoyi.system.domain.equipment.EquipmentBook;
import com.ruoyi.system.domain.equipment.EquipmentBookDTO;
import com.ruoyi.system.domain.equipment.EquipmentByzx;
import com.ruoyi.system.domain.equipment.EquipmentType;
import com.ruoyi.system.mapper.BaseShopMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.equipment.EquipmentBookMapper;
import com.ruoyi.system.mapper.equipment.EquipmentByzxMapper;
import com.ruoyi.system.mapper.equipment.EquipmentTypeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IEquipmentBookService;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 设备台账Service业务层处理
 *
 * @author ruoyi
 * @date 2023-03-01
 */
@Service
public class EquipmentBookServiceImpl extends ServiceImpl<EquipmentBookMapper, EquipmentBook> implements IEquipmentBookService {

    @Autowired
    private BaseShopMapper baseShopMapper;
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private EquipmentTypeMapper equipmentTypeMapper;
    @Autowired
    private EquipmentByzxMapper equipmentByzxMapper;

    @Override
    public List<EquipmentBookDTO> getList(EquipmentBook equipmentBook) {
        List<EquipmentBookDTO> list = baseMapper.getList(equipmentBook);
        return list;
    }

    @Override
    public EquipmentBookDTO getInfoById(Long bookId) {
        EquipmentBookDTO dto = baseMapper.getInfoById(bookId);
        return dto;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addSbtz(EquipmentBook equipmentBook) {
        equipmentBook.setCreateBy(SecurityUtils.getUsername());
        equipmentBook.setCreateTime(new Date());
        BaseShop shop = baseShopMapper.selectById(equipmentBook.getShopId());
        equipmentBook.setShopName(shop.getShopName());
        SysDept sysDept = sysDeptMapper.selectDeptById(equipmentBook.getMakeDeptId());
        equipmentBook.setMakeDeptName(sysDept.getDeptName());
        SysDept sysDept1 = sysDeptMapper.selectDeptById(equipmentBook.getStandDeptId());
        equipmentBook.setStandDeptName(sysDept1.getDeptName());
        baseMapper.insert(equipmentBook);

        //同步新增设备保养执行数据
        EquipmentByzx equipmentByzx = new EquipmentByzx();
        equipmentByzx.setEquipmentCode(equipmentBook.getBookCode());
        equipmentByzx.setEquipmentName(equipmentBook.getBookName());
        EquipmentType equipmentType = equipmentTypeMapper.selectById(equipmentBook.getTypeId());
        equipmentByzx.setEquipmentType(String.valueOf(equipmentType.getTypeId()));
        equipmentByzx.setEquipmentTypeName(equipmentType.getTypeName());
        equipmentByzx.setPlanDate(equipmentBook.getApplyDate());
        equipmentByzx.setByData("校准");
        equipmentByzx.setByType("校准");
        equipmentByzx.setEquipmentId(equipmentBook.getBookId());
        equipmentByzx.setByLx(1);
        equipmentByzx.setCreateTime(new Date());
        equipmentByzx.setCreateBy(SecurityUtils.getUsername());
        equipmentByzxMapper.insert(equipmentByzx);

        return AjaxResult.success("新增成功");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult updateSbtz(EquipmentBook equipmentBook) {
        equipmentBook.setUpdateBy(SecurityUtils.getUsername());
        equipmentBook.setUpdateTime(new Date());
        baseMapper.updateById(equipmentBook);

        //同步更新设备保养执行数据
        EquipmentByzx equipmentByzx = new EquipmentByzx();
        equipmentByzx.setEquipmentCode(equipmentBook.getBookCode());
        equipmentByzx.setEquipmentName(equipmentBook.getBookName());
        EquipmentType equipmentType = equipmentTypeMapper.selectById(equipmentBook.getTypeId());
        equipmentByzx.setEquipmentType(String.valueOf(equipmentType.getTypeId()));
        equipmentByzx.setEquipmentTypeName(equipmentType.getTypeName());
        equipmentByzx.setPlanDate(equipmentBook.getApplyDate());
        equipmentByzx.setEquipmentId(equipmentBook.getBookId());
        equipmentByzx.setUpdateTime(new Date());
        equipmentByzx.setUpdateBy(SecurityUtils.getUsername());
        equipmentByzxMapper.update(equipmentByzx,new QueryWrapper<EquipmentByzx>()
                .eq("equipment_id",equipmentBook.getBookId()));
        return AjaxResult.success("更新成功");
    }
}
